<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="root">

    </div>
</body>
<script src="../react@17.js"></script>
<script src="../react-dom@17.js"></script>
<script src="../babel.js"></script>
<script type="text/babel">
    class App extends React.Component{
        state={
            list:[
                {
                    name:"辣条",
                    price:5,
                    num:1
                },{
                    name:"巧克力",
                    price:35,
                    num:2
                },{
                    name:"奶茶",
                    price:15,
                    num:1
                },{
                    name:"烤箱",
                    price:2099,
                    num:1
                }
            ]
        }
        render(){
            let {list} = this.state;
            return (
                <div>
                    <table border='1'>
                     <thead>
                        <tr>
                            <th>名称</th>
                            <th>价格</th>
                            <th>数量</th>
                            <th>操作</th>
                        </tr>
                    </thead>   
                    <tbody>
                        {
                            list.map((item,index)=>{
                                return (
                                    <tr key={index}>
                                        <td>{item.name}</td>
                                        <td>{item.price}</td>
                                        <td>
                                        <button style={{
                                            background:item.num===0?'red':''
                                        }} onClick={()=>this.reducer(index)}>-</button>
                                        {item.num} 
                                        <button onClick={()=>this.add(index)} style={{
                                            background:item.num===10?'red':''
                                        }}>+</button>   
                                        </td>
                                        <td>
                                            <button onClick={()=>{
                                                list.splice(index,1);
                                                this.setState({
                                                    list:list
                                                })
                                            }}>删除</button>    
                                        </td>    
                                    </tr>
                                )
                            })
                        }
                    </tbody> 
                    </table>    
                    <h1>商品总价：{this.getSum()}</h1>
                </div>
            )
        }
        getSum(){
            // 求商品总价=单个商品总价之和
            // 单个商品总价= 商品单价 * 商品数量
            let sum = 0;
            let {list} = this.state;
            list.forEach((item)=>{
                let num = item.price * item.num;
                console.log(num)
                sum+=num;
            })
            // console.log(sum)
            return sum;
        }
        add(index){
            let {list} = this.state;
            // 根据索引获取商品对象
            let goods = list[index];
            // 判断当前商品数量是否大于10
            if(goods.num>=10){
                return
            }
            goods.num++;
            this.setState({
                list:list
            })
        }
        reducer(index){
            let {list} = this.state;
            // 根据索引获取商品对象
            let goods = list[index];
            // 判断当前商品数量是否大于10
            if(goods.num<=0){
                return
            }
            goods.num--;
            this.setState({
                list:list
            })
        }
    }

    ReactDOM.render(<App/>,document.getElementById("root"))
</script>
</html>